namespace CRM_Perf_BenchMark
{
	using System;
	using System.Collections.Generic;
	using Microsoft.VisualStudio.TestTools.WebTesting;
	using System.Net;
	using System.Text;
	using System.Xml;
	
    /// <summary>
    /// 1. Start at Service Calendar homepage
    /// 2. Select a day without any appointments on it
    /// </summary>

	public class DisplayWorkHours : CrmWebTest
	{
		public void DisplayWorkHours_PreWebTest(object sender, PreWebTestEventArgs e)
		{
            EntityRequest er = new EntityRequest();
            er.Type = EntityNames.Users;
            er.ReturnAs = EntityNames.Users;
            er.ParentID = EntityManager.Instance.OrgId;

            System.Collections.Hashtable userProps = new System.Collections.Hashtable();
            if (e.WebTest.Context.ContainsKey("UserRole"))
            {
                userProps.Add("role", e.WebTest.Context["UserRole"]);
                er.Props = userProps;
            }

            System.Collections.Hashtable Entities = EntityManager.Instance.GetEntities(er);

            user = (CRMEntity)Entities[EntityNames.Users];

            crmServer = user["CrmServer"];
            tokenKey = EntityManager.Instance.ReadTokenKey(user["organizationid"]);

            e.WebTest.UserName = user["domainName"];
            e.WebTest.Password = user["userpassword"];
		}
		
        public void DisplayWorkHours_PostWebTest(object sender, PostWebTestEventArgs e)
		{
			EntityManager.Instance.FreeEntity(user);
		}

		public DisplayWorkHours()
		{
            WebRequest.RegisterPrefix("crm", new crmRequestFactory());
			this.Proxy = null;            
			PreWebTest += new EventHandler<PreWebTestEventArgs>(DisplayWorkHours_PreWebTest);
			PostWebTest += new EventHandler<PostWebTestEventArgs>(DisplayWorkHours_PostWebTest);
		}

		static DisplayWorkHours()
		{
		}

		public override IEnumerator<WebTestRequest> GetRequestEnumerator()
		{
			this.BeginTransaction("DisplayWorkHours");

            DateTimeService.DateTimeWebService dtws = new DateTimeService.DateTimeWebService();
            CrmRequest wtr = null;
            string currDate = DateTime.Today.ToString();
            try { dtws.GetFormattedDate(currDate); }
            catch (crmException x)
            {
                wtr = (CrmRequest)x.wtr;
                wtr.Url = Utils.ReplaceServerName(wtr.Url, user);
            }
            yield return (wtr);
            yield return (wtr);
            yield return (wtr);
            yield return (wtr);
            yield return (wtr);
            yield return (wtr);
            yield return (wtr);

            sm_ganttcontrolframe_aspx smgantcontrol = new sm_ganttcontrolframe_aspx(user);
            smgantcontrol.startDate = DateTime.Today.AddYears(1);
            smgantcontrol.endDate = DateTime.Today.AddYears(1).AddDays(1);
            smgantcontrol.subareatype = "schedules";
            smgantcontrol.viewID = new Guid("7738C997-2681-43BE-BFCF-DB1D159B5C5A");
            smgantcontrol.zoomLevel = 4;
            yield return (smgantcontrol);

            int index = smgantcontrol.lastResponse.BodyString.IndexOf(indexPointer);
            string idString = smgantcontrol.lastResponse.BodyString.Substring(index + indexPointer.Length, 36);
            Guid id = new Guid(idString);

            sm_detailsappointmentpane_aspx smdetails = new sm_detailsappointmentpane_aspx(user);
            smdetails.subAreaType = "schedules";
            smdetails.rowType = 4000;
            smdetails.rowId = id;
            yield return (smdetails);

            index = smdetails.lastResponse.BodyString.IndexOf(indexPtr);
            string customizableFormQueryString = smdetails.lastResponse.BodyString.Substring(index + indexPtr.Length, smdetails.lastResponse.BodyString.IndexOf('"', index + indexPtr.Length) - (index + indexPtr.Length));
            string tabIDString = customizableFormQueryString.Substring(customizableFormQueryString.IndexOf("tabID={") + 7, 36);
            Guid tabID = new Guid(tabIDString);

            sm_customizableform_aspx smcustomform = new sm_customizableform_aspx(user);
            smcustomform.type = 4000;
            smcustomform.id = id;
            smcustomform.tabID = tabID;
            yield return (smcustomform);

            referer = smcustomform.lastResponse.ResponseUri.ToString();

            _forms_FormScript_js_aspx fs = new _forms_FormScript_js_aspx(user);
            fs.etc = 4000;
            fs.FormType = "appointmentBook";
            fs.FormName = "crmForm";
            headerItem = new WebTestRequestHeader();
            headerItem.Name = "referer";
            headerItem.Value = referer;
            fs.Headers.Add(headerItem);
            yield return (fs);

			this.EndTransaction("DisplayWorkHours");
		}
		private CRMEntity user;
        private string crmServer;
        private string tokenKey;
        private string referer;
        private WebTestRequestHeader headerItem;
        private string indexPtr = @"var pagesNames = new Array(""customizableForm.aspx?";
        private string indexPointer = @"name=""businessunitid"" CLASS=""ms-crm-List-DataColumn""><col><tr class=""ms-crm-List-Row"" oid=""&#123;";
	}
}